package com.dw.etl.core.db;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

/**
 * 动态数据源（数据源切换）
 * Created by hoze on 2017/1/15.
 */
public class DynamicDataSource extends AbstractRoutingDataSource {

    private final static Logger LOGGER = LoggerFactory.getLogger(DynamicDataSource.class);

    public static Map<Object,Object> dataSourceMap=new HashMap<Object, Object>();

    public DynamicDataSource() {
    }

    @Override
    protected Object determineCurrentLookupKey() {
        String dataSource = ThreadLocalDataSource.getLocalSource();
        if(dataSource==null) {
            dataSource = DataSourceConfig.DEFAULT_DATABASE;
        }
        return dataSource;
    }

    @Override
    public void setTargetDataSources(Map<Object, Object> targetDataSources) {
        super.setTargetDataSources(targetDataSources);
        super.afterPropertiesSet();
    }
}
